/**
  ******************************************************************************
  * @file    main.c
  * @author  Cris
  * @version V1.0.0
  * @date    2017-12-29
  * @brief   This file contains the battery-charger/power provider functions
  ******************************************************************************
  * @attention
  *
  *     key     :i < mannual control
  *     DC_AD   :i < load voltage
  *     BATT_AD :i < battery voltage
  *     CHRG#   :i < Low indicate in charging
  *     DONE#   :i < Low indicate charge finish
  *     DCOK    :o > Low shutdown charge-forward
  *     CH_EN   :o > Low shutdown charger
  *     BATT_OUT:o > High open power output
  *     PWRKEY  :o > launch MC20-modem
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
/**
  * @addtogroup GPIO_Toggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#include "system.h"

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
    
/* Private function prototypes -----------------------------------------------*/
void Delay (uint16_t nCount);
void uart_sendbyte(uint8_t data);
void digit2char(uint8_t* ch);
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
BitStatus kf;
uint16_t dt=0x40;
#define SOFT_CTRL_KEY 1

void main(void)
{
    /*High speed internal clock prescaler: 1*/
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  softimer_init();
  fifo_init();
  rilinit();
  /* Initialize I/Os in Output Mode */
  GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
  
#if SOFT_CTRL_KEY
  GPIO_Init(MC20_GPIO_PORT, (GPIO_Pin_TypeDef)MC20_PWRK_PINS, GPIO_MODE_OUT_PP_LOW_FAST); //MC20 PWRKEY
#else
  GPIO_Init(MC20_GPIO_PORT, (GPIO_Pin_TypeDef)MC20_PWRK_PINS, GPIO_MODE_IN_PU_NO_IT);
#endif
  
  GPIO_Init(SOFT_SEND_PORT, (GPIO_Pin_TypeDef)SOFT_SEND_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);//SOFT SEND
  //event_alarm_init();
  
  //key_init();
  //adc_config(0); //single channel
  
  TIM2_DeInit();
  TIM4_DeInit();
  
  TIM2_TimeBaseInit(TIM2_PRESCALER_2, BR9600_PRESET);
  TIM2_UpdateDisableConfig(DISABLE);
  TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_REGULAR);
  TIM2_SelectOnePulseMode(TIM2_OPMODE_SINGLE);
  TIM2_ARRPreloadConfig(DISABLE);
  TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
      
  TIM4_TimeBaseInit(TIM4_PRESCALER_128, 255);
  TIM4_Cmd(ENABLE);
  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  
  UART1_DeInit();
  /* UART1 configuration ------------------------------------------------------*/
  /* UART1 configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
        - UART1 Clock disabled
  */
    UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
                UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
    UART1_ITConfig(UART1_IT_TXE, DISABLE);
    UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
      
    //GPIO_WriteLow(MC20_GPIO_PORT, (GPIO_Pin_TypeDef)MC20_PWRK_PINS); //launch MC20
    //event_alarm_set(5);
  /* Enable general interrupts */
#if SOFT_CTRL_KEY
     enableInterrupts();
#endif
      soft_send_byte(0xAA);      //skip first unstable signal
    while (1)
    {
//        adc_monitor();
        rilloop();
        uart_recv_monitor();
        soft_send_monitor();
//        getkeycode();
//        check_charge();
        //loopshow();
        //Delay(dt);
        //event_alarm_trig();
    }

}

/**
  * @brief Delay
  * @param nCount
  * @retval None
  */
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

void uart_sendbyte(uint8_t data)
{
  FlagStatus sf;
  do{
    sf = UART1_GetFlagStatus(UART1_FLAG_TXE);
  }while(!sf);
  UART1_SendData8(data);
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
